// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
description: >
    Throw a TypeError exception if trap result is true, targetDesc is not configurable
    and writable, while Desc is not writable.
info: |
    [[DefineOwnProperty]] (P, Desc)

    ...
    16. Else targetDesc is not undefined,
        ...
        false, and targetDesc.[[Writable]] is true, then
            i. If Desc has a [[Writable]] field and Desc.[[Writable]] is
            false, throw a TypeError exception.
    ...
features: [Proxy, Reflect, proxy-missing-checks]
---*/

function Test() {
    var trapCalls = 0;
    var p = new Proxy({}, {
      defineProperty: function(t, prop, desc) {
        Object.defineProperty(t, prop, {
          configurable: false,
          writable: true,
        });

        trapCalls++;
        return true;
      },
    });

    assert.throws(TypeError, function() {
      Reflect.defineProperty(p, "prop", {
        writable: false,
      });
    });
    assert.sameValue(trapCalls, 1);
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
